package cn.caplike.demo.repository.design.pattern.eventbus.guava;

import com.google.common.eventbus.AsyncEventBus;
import com.google.common.eventbus.EventBus;
import com.google.common.eventbus.SubscriberExceptionContext;
import com.google.common.eventbus.SubscriberExceptionHandler;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

@SuppressWarnings("UnstableApiUsage")
public class Client {

    public static void main(String[] args) {
        System.out.println("currentThreadId: " + Thread.currentThread().getId());
        final EventBus eventBus = new EventBus(new SubscriberExceptionHandler() {
            @Override
            public void handleException(Throwable exception, SubscriberExceptionContext context) {
                System.err.println("exception: " + exception.getMessage());
            }
        });
        eventBus.register(new EventListener());

//        eventBus.post(1);
//        eventBus.post("1");

        eventBus.post(new RuntimeException());

//        final ExecutorService executor = Executors.newCachedThreadPool();
//        final EventBus asyncEventBus = new AsyncEventBus(executor);
//        asyncEventBus.register(new EventListener());
//        asyncEventBus.post(1);
//        asyncEventBus.post("1");
//        executor.shutdown();
    }
}
